package cn.org.xiaoweiba.graduationdesign.bookmall.aspect;

import cn.org.xiaoweiba.graduationdesign.bookmall.constant.ResponseResultMessageConstant;
import cn.org.xiaoweiba.graduationdesign.bookmall.dto.result.ServerErrorResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 服务器异常捕获处理切面类
 */
@Aspect
@Order(1)
@Component
public class ServerErrorAspect {

    @Pointcut("@annotation(cn.org.xiaoweiba.graduationdesign.bookmall.annotation.error.ServerError)")
    private void pointCut() {}

    /**
     * 捕获服务器异常环绕通知
     *
     * @param proceedingJoinPoint 切点
     * @return 切点方法执行返回结果
     */
    @Around("pointCut()")
    public Object catchServerErrorAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            // 执行切点方法，并返回切点方法执行返回结果
            return proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            // 发生异常响应服务器错误响应结果对象
            return ServerErrorResponseResult.errorResult(
                    ResponseResultMessageConstant.SERVER_ERROR
            );
        }
    }

}
